//
//  MyOrderCell.m
//  reSearchDemo
//
//  Created by 科技 on 15/7/28.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MyOrderCell.h"
#import "MyOrderModel.h"
#import "UIView+Layer.h"
@interface MyOrderCell()
{
    UIImageView *_homeImageV;    /**< 商家头像 */
    UIImageView *_arrowImageV;   /**< 箭头 */
    UILabel *_text;              /**< 商家名字 */
    UILabel *_state;             /**< 订单状态 */
    UIView *_ContextView;        /**< 内容view */
    UIImageView *_headImageV;    /**< 内容图片 */
    UILabel *_context;           /**< 内容 */
    UILabel *_money;             /**< 商品单价 */
    UILabel *_number;            /**< 商品数量 */
    UILabel *_insurance;         /**< 保险业务text */
    UILabel *_insuranceMoney;    /**< 保险单价 */
    UILabel *_insuranceNumber;   /**< 保险数量 */
    UIView *_line;               /**< 下划线 */
    UILabel *_AllNumber;         /**< 总价描述 */
    UIView *_lineView;           /**< 下划线 */
}

@end

@implementation MyOrderCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self AllocView];
    }
    
    return self;
}

-(void)AllocView {
    _homeImageV = [[UIImageView alloc] init];
    _text = [[UILabel alloc] init];
    _arrowImageV = [[UIImageView alloc] init];
    _state = [[UILabel alloc] init];
    _state.font = [UIFont systemFontOfSize:14];
    _state.textAlignment = NSTextAlignmentRight;
    _state.textColor = [UIColor orangeColor];
    
    [self.contentView addSubview:_homeImageV];
    [self.contentView addSubview:_text];
    [self.contentView addSubview:_arrowImageV];
    [self.contentView addSubview:_state];
    
    _ContextView = [[UIView alloc] init];
//    _ContextView.backgroundColor = [UIColor lightGrayColor];
    
    _headImageV = [[UIImageView alloc] init];
    
    _context = [[UILabel alloc] init];
    _context.numberOfLines = 0;
    _context.font = [UIFont systemFontOfSize:14];
    _context.textColor = [UIColor blackColor];
    
    _money = [[UILabel alloc] init];
    _money.textAlignment = NSTextAlignmentRight;
    _money.textColor = [UIColor blackColor];
    _money.font = [UIFont systemFontOfSize:14];
    
    _number = [[UILabel alloc] init];
    _number.textAlignment = NSTextAlignmentRight;
    _number.textColor = [UIColor grayColor];
    _number.font = [UIFont systemFontOfSize:14];
    
    _insurance = [[UILabel alloc] init];
    _insurance.textAlignment = NSTextAlignmentCenter;
    _insurance.textColor = [UIColor blackColor];
    _insurance.font = [UIFont systemFontOfSize:14];
    
    _insuranceMoney = [[UILabel alloc] init];
    _insuranceMoney.textAlignment = NSTextAlignmentRight;
    _insuranceMoney.textColor = [UIColor blackColor];
    _insuranceMoney.font = [UIFont systemFontOfSize:14];
    
    _insuranceNumber = [[UILabel alloc] init];
    _insuranceNumber.textAlignment = NSTextAlignmentRight;
    _insuranceNumber.textColor = [UIColor lightGrayColor];
    _insuranceNumber.font = [UIFont systemFontOfSize:14];
    
    _line = [[UIView alloc] init];
    _line.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:0.9];
    
    _AllNumber = [[UILabel alloc] init];
    _AllNumber.textAlignment = NSTextAlignmentRight;
    _AllNumber.textColor = [UIColor blackColor];
    _AllNumber.font = [UIFont systemFontOfSize:14];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:0.9];
    
    _btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_btn1 setBackgroundColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:0.9]];
    [_btn1 setTitleColor:[UIColor blackColor] forState:0];
    [UIView SetRoundedCorners:_btn1 andcornerRadius:5.0 andborderWidth:1 andborderColor:[UIColor clearColor]];
    
    _btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_btn2 setBackgroundColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:0.9]];
    [_btn2 setTitleColor:[UIColor blackColor] forState:0];
    [UIView SetRoundedCorners:_btn2 andcornerRadius:5.0 andborderWidth:1 andborderColor:[UIColor clearColor]];
    
    _btn3 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_btn3 setBackgroundColor:[UIColor orangeColor]];
    [_btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [UIView SetRoundedCorners:_btn3 andcornerRadius:5.0 andborderWidth:1 andborderColor:[UIColor clearColor]];
    
    [_ContextView addSubview:_headImageV];
    [_ContextView addSubview:_context];
    [_ContextView addSubview:_money];
    [_ContextView addSubview:_number];
    [self.contentView addSubview:_insurance];
    [self.contentView addSubview:_insuranceMoney];
    [self.contentView addSubview:_insuranceNumber];
    [self.contentView addSubview:_line];
    [self.contentView addSubview:_AllNumber];
    [self.contentView addSubview:_lineView];
    [self.contentView addSubview:_ContextView];
    [self.contentView addSubview:_btn1];
    [self.contentView addSubview:_btn2];
    [self.contentView addSubview:_btn3];
}

-(void)setModel:(MyOrderModel *)model {
    
    _height = 0;
    
    _homeImageV.frame = CGRectMake(5, 10, 10, 10);
    _homeImageV.image = [UIImage imageNamed:@"icon_public.png"];
    _text.frame = CGRectMake(CGRectGetMaxX(_homeImageV.frame) + 5, 5, 60, 20);
    _text.text = model.MerchantsName;
    _text.font = [UIFont systemFontOfSize:12];
    _arrowImageV.frame = CGRectMake(CGRectGetMaxX(_text.frame), 10, 10, 10);
    _arrowImageV.image = [UIImage imageNamed:@"arrow_right.png"];
    _state.frame = CGRectMake(CGRectGetWidth(self.frame) - 200, 5, 190, 20);
    _state.text = model.TransactionStatus;
    
    _height += 25;
    
    _ContextView.frame = CGRectMake(0, CGRectGetMinY(_text.frame) + 5, CGRectGetWidth(self.frame), 80);
    _headImageV.frame = CGRectMake(10, 20, 60, 60);
    _headImageV.image = [UIImage imageNamed:@"about_icon.png"];
    CGSize textSize=[model.Context boundingRectWithSize:CGSizeMake(200, 588) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]} context:nil].size;
    if (textSize.height > 80) {
        _context.frame = CGRectMake(CGRectGetMaxX(_headImageV.frame) + 10, 10, textSize.width, textSize.height);
        _height += textSize.height + 5;
    }else {
        _context.frame = CGRectMake(CGRectGetMaxX(_headImageV.frame) + 10 , 10, 180, 80);
        _height += 80 + 5;
    }
    _context.text = model.Context;
    _money.frame = CGRectMake(CGRectGetWidth(self.frame) - 80, CGRectGetMinY(_headImageV.frame), 70, 20);
    _money.text = model.Money;
    _number.frame = CGRectMake(CGRectGetWidth(self.frame) - 60, CGRectGetMaxY(_money.frame), 50, 20);
    _number.text = model.Number;
    
    switch (self.celltype) {
        case AllCellType:
        {
            switch ([model.Type intValue]) {
                case 1:
                {
                    [self secondCellType];
                    break;
                }
                case 2:
                {
                    [self ThirdCellType];
                    break;
                }
                case 3:
                {
                    [self fourthCellType];
                    break;
                }
                case 4:
                {
                    [self fifthCellType];
                    break;
                }
                    
                default:
                    break;
            }
           break;
        }
        case secondCellType:
        {
            [self secondCellType];
            break;
        }
        case ThirdCellType:
        {
            [self ThirdCellType];
            break;
        }
        case fourthCellType:
        {
            [self fourthCellType];
            break;
        }
        case fifthCellType:
        {
            [self fifthCellType];
            break;
        }
            
            
        default:
            break;
    }
    
}

#pragma mark --4种类型cell的创建
//待付款类型
-(void)secondCellType {
    //清空控件
    [self removeCellView];
    
    _insurance.frame = CGRectMake(10, CGRectGetMaxY(_ContextView.frame) + 10, 80, 20);
    _insurance.text = @"保险服务";
    
    _insuranceMoney.frame = CGRectMake(CGRectGetWidth(self.frame) - 120, CGRectGetMinY(_insurance.frame), 110, 20);
    _insuranceMoney.text = @"￥0.00";
    
    _insuranceNumber.frame = CGRectMake(CGRectGetWidth(self.frame) - 80, CGRectGetMaxY(_insuranceMoney.frame), 70, 20);
    _insuranceNumber.text = @"1";
    
    _line.frame = CGRectMake(15, CGRectGetMaxY(_insuranceNumber.frame), CGRectGetWidth(self.frame) - 15, 1);
    
    _height += 30;
    
    _AllNumber.frame = CGRectMake(0, CGRectGetMaxY(_insuranceNumber.frame) + 10, CGRectGetWidth(self.frame) - 10, 20);
    _AllNumber.text = [NSString stringWithFormat:@"共%d件商品 合计:￥%.2f元(含运费￥%.2f)",[_model.ObjectNumber intValue],[_model.AllMoney floatValue],[_model.Freight floatValue]];
    _height += 30;
    
    _lineView.frame = CGRectMake(15, CGRectGetMaxY(_AllNumber.frame) + 5, CGRectGetWidth(self.frame) - 15, 1);
    
    _btn3.frame = CGRectMake(CGRectGetWidth(self.frame) - 60, CGRectGetMaxY(_lineView.frame) + 10, 50 , 30);
    [_btn3 setTitle:@"付款" forState:UIControlStateNormal];
    
    _btn2.frame = CGRectMake(CGRectGetMinX(_btn3.frame) - 70, CGRectGetMinY(_btn3.frame), 60, 30);
    [_btn2 setTitle:@"取消订单" forState:UIControlStateNormal];
    
    _btn1.frame = CGRectMake(CGRectGetMinX(_btn2.frame) - 70, CGRectGetMinY(_btn3.frame), 60, 30);
    [_btn1 setTitle:@"联系卖家" forState:UIControlStateNormal];
    
    _height += 50;
    
}
//待发货类型
-(void)ThirdCellType {
    //清空控件
    [self removeCellView];
}
//待收货类型
-(void)fourthCellType {
    //清空控件
    [self removeCellView];
    _insurance.frame = CGRectMake(10, CGRectGetMaxY(_ContextView.frame) + 10, 80, 20);
    _insurance.text = @"保险服务";
    
    _insuranceMoney.frame = CGRectMake(CGRectGetWidth(self.frame) - 120, CGRectGetMinY(_insurance.frame), 110, 20);
    _insuranceMoney.text = @"￥0.00";
    
    _insuranceNumber.frame = CGRectMake(CGRectGetWidth(self.frame) - 80, CGRectGetMaxY(_insuranceMoney.frame), 70, 20);
    _insuranceNumber.text = @"1";
    
    _line.frame = CGRectMake(15, CGRectGetMaxY(_insuranceNumber.frame), CGRectGetWidth(self.frame) - 15, 1);
    
    _height += 30;
    
    _AllNumber.frame = CGRectMake(0, CGRectGetMaxY(_insuranceNumber.frame) + 10, CGRectGetWidth(self.frame) - 10, 20);
    _AllNumber.text = [NSString stringWithFormat:@"共%d件商品 合计:￥%.2f元(含运费￥%.2f)",[_model.ObjectNumber intValue],[_model.AllMoney floatValue],[_model.Freight floatValue]];
    _height += 30;
    
    _lineView.frame = CGRectMake(15, CGRectGetMaxY(_AllNumber.frame) + 5, CGRectGetWidth(self.frame) - 15, 1);
    
    _btn3.frame = CGRectMake(CGRectGetWidth(self.frame) - 70, CGRectGetMaxY(_lineView.frame) + 10, 60 , 30);
    [_btn3 setTitle:@"确认收货" forState:UIControlStateNormal];
    
    _btn2.frame = CGRectMake(CGRectGetMinX(_btn3.frame) - 70, CGRectGetMinY(_btn3.frame), 60, 30);
    [_btn2 setTitle:@"查看物流" forState:UIControlStateNormal];
    
    
    _height += 50;
    
}
//待评价类型
-(void)fifthCellType {
    //清空控件
    [self removeCellView];
    _AllNumber.frame = CGRectMake(0, CGRectGetMaxY(_ContextView.frame) + 10, CGRectGetWidth(self.frame) - 10, 20);
    _AllNumber.text = [NSString stringWithFormat:@"共%d件商品 合计:￥%.2f元(含运费￥%.2f)",[_model.ObjectNumber intValue],[_model.AllMoney floatValue],[_model.Freight floatValue]];
    _height += 30;
    
    _lineView.frame = CGRectMake(15, CGRectGetMaxY(_AllNumber.frame) + 5, CGRectGetWidth(self.frame) - 15, 1);
    
    _btn3.frame = CGRectMake(CGRectGetWidth(self.frame) - 60, CGRectGetMaxY(_lineView.frame) + 10, 50 , 30);
    [_btn3 setTitle:@"评价" forState:UIControlStateNormal];
    
    _btn2.frame = CGRectMake(CGRectGetMinX(_btn3.frame) - 70, CGRectGetMinY(_btn3.frame), 60, 30);
    [_btn2 setTitle:@"删除订单" forState:UIControlStateNormal];
    
    
    _height += 40;
}

#pragma mark --清空当前cell的样式--
-(void)removeCellView {
    _insurance.frame = CGRectZero;
    _insurance.text = nil;
    
    _insuranceMoney.frame = CGRectZero;
    _insuranceMoney.text = nil;
    
    _insuranceNumber.frame = CGRectZero;
    _insuranceNumber.text = nil;
    
    _line.frame = CGRectZero;
    
    _AllNumber.frame = CGRectZero;
    _AllNumber.text = nil;
    
    _lineView.frame = CGRectZero;
    
    _btn1.frame = CGRectZero;
    _btn1.titleLabel.text = nil;
    [_btn1 removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
    
    _btn2.frame = CGRectZero;
    _btn2.titleLabel.text = nil;
    [_btn2 removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
    
    _btn3.frame = CGRectZero;
    _btn3.titleLabel.text = nil;
    [_btn3 removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
